home *** CD-ROM | disk | FTP | other *** search
/ TPUG - Toronto PET Users Group / TPUG Users Group CD / TPUG Users Group CD.iso / AMIGA / AMICUS / AMICUS05.ADF / IFF / ilbm.h < prev    next >
C/C++ Source or Header  |  1986-04-20  |  11KB  |  263 lines

  1.  
  2. #ifndef ILBM_H
  3. #define ILBM_H
  4. /*----------------------------------------------------------------------*
  5.  * ILBM.H  Definitions for InterLeaved BitMap raster image.     1/23/86
  6.  *
  7.  * By Jerry Morrison and Steve Shaw, Electronic Arts.
  8.  * This software is in the public domain.
  9.  *
  10.  * This version for the Commodore-Amiga computer.
  11.  *----------------------------------------------------------------------*/
  12. #ifndef COMPILER_H
  13. #include "iff/compiler.h"
  14. #endif
  15.  
  16. #ifndef GRAPHICS_GFX_H
  17. #include "graphics/gfx.h"
  18. #endif
  19.  
  20. #include "iff/iff.h"
  21.  
  22. #define ID_ILBM MakeID('I','L','B','M')
  23. #define ID_BMHD MakeID('B','M','H','D')
  24. #define ID_CMAP MakeID('C','M','A','P')
  25. #define ID_GRAB MakeID('G','R','A','B')
  26. #define ID_DEST MakeID('D','E','S','T')
  27. #define ID_SPRT MakeID('S','P','R','T')
  28. #define ID_CAMG MakeID('C','A','M','G')
  29. #define ID_BODY MakeID('B','O','D','Y')
  30.  
  31. /* ---------- BitMapHeader ---------------------------------------------*/
  32.  
  33. typedef UBYTE Masking;        /* Choice of masking technique.*/
  34. #define mskNone                 0
  35. #define mskHasMask              1
  36. #define mskHasTransparentColor  2
  37. #define mskLasso                3
  38.  
  39. typedef UBYTE Compression;    /* Choice of compression algorithm applied to
  40.      * each row of the source and mask planes. "cmpByteRun1" is the byte run
  41.      * encoding generated by Mac's PackBits. See Packer.h . */
  42. #define cmpNone      0
  43. #define cmpByteRun1  1
  44.  
  45. /* Aspect ratios: The proper fraction xAspect/yAspect represents the pixel
  46.  * aspect ratio pixel_width/pixel_height.
  47.  *
  48.  * For the 4 Amiga display modes:
  49.  *   320 x 200: 10/11  (these pixels are taller than they are wide)
  50.  *   320 x 400: 20/11
  51.  *   640 x 200:  5/11
  52.  *   640 x 400: 10/11         */
  53. #define x320x200Aspect 10
  54. #define y320x200Aspect 11
  55. #define x320x400Aspect 20
  56. #define y320x400Aspect 11
  57. #define x640x200Aspect  5
  58. #define y640x200Aspect 11
  59. #define x640x400Aspect 10
  60. #define y640x400Aspect 11
  61.  
  62. /* A BitMapHeader is stored in a BMHD chunk. */
  63. typedef struct {
  64.     UWORD w, h;               /* raster width & height in pixels */
  65.     WORD  x, y;               /* position for this image */
  66.     UBYTE nPlanes;       /* # source bitplanes */
  67.     Masking masking;          /* masking technique */
  68.     Compression compression;  /* compression algoithm */
  69.     UBYTE pad1;               /* UNUSED.  For consistency, put 0 here.*/
  70.     UWORD transparentColor;   /* transparent "color number" */
  71.     UBYTE xAspect, yAspect;   /* aspect ratio, a rational number x/y */
  72.     WORD  pageWidth, pageHeight;  /* source "page" size in pixels */
  73.     } BitMapHeader;
  74.  
  75. /* RowBytes computes the number of bytes in a row, from the width in pixels.*/
  76. #define RowBytes(w)   (((w) + 15) >> 4 << 1)
  77.  
  78.  
  79. /* ---------- ColorRegister --------------------------------------------*/
  80. /* A CMAP chunk is a packed array of ColorRegisters (3 bytes each). */
  81. typedef struct {
  82.     UBYTE red, green, blue;   /* MUST be UBYTEs so ">> 4" won't sign extend.*/
  83.     } ColorRegister;
  84.  
  85. /* Use this constant instead of sizeof(ColorRegister). */
  86. #define sizeofColorRegister  3
  87.  
  88. typedef WORD Color4;     /* Amiga RAM version of a color-register,
  89.                 * with 4 bits each RGB in low 12 bits.*/
  90.  
  91. /* Maximum number of bitplanes in RAM. Current Amiga max w/dual playfield. */
  92. #define MaxAmDepth 6
  93.  
  94. /* ---------- Point2D --------------------------------------------------*/
  95. /* A Point2D is stored in a GRAB chunk. */
  96. typedef struct {
  97.     WORD x, y;      /* coordinates (pixels) */
  98.     } Point2D;
  99.  
  100. /* ---------- DestMerge ------------------------------------------------*/
  101. /* A DestMerge is stored in a DEST chunk. */
  102. typedef struct {
  103.     UBYTE depth;    /* # bitplanes in the original source */
  104.     UBYTE pad1;          /* UNUSED; for consistency store 0 here */
  105.     UWORD planePick;     /* how to scatter source bitplanes into destination */
  106.     UWORD planeOnOff;    /* default bitplane data for planePick */
  107.     UWORD planeMask;     /* selects which bitplanes to store into */
  108.     } DestMerge;
  109.  
  110. /* ---------- SpritePrecedence -----------------------------------------*/
  111. /* A SpritePrecedence is stored in a SPRT chunk. */
  112. typedef UWORD SpritePrecedence;
  113.  
  114. /* ---------- Viewport Mode --------------------------------------------*/
  115. /* A Commodore Amiga ViewPort->Modes is stored in a CAMG chunk. */
  116. /* The chunk's content is declared as a LONG. */
  117.  
  118. /* ---------- CRange ---------------------------------------------------*/
  119. /* A CRange is store in a CRNG chunk. */
  120. typedef struct {
  121.     WORD  pad1;          /* reserved for future use; store 0 here */
  122.     WORD  rate;          /* color cycling rate, 16384 = 60 steps/second */
  123.     WORD  active;   /* nonzero means color cycling is turned on */
  124.     UBYTE low, high;     /* lower and upper color registers selected */
  125.     } CRange;
  126.  
  127. /* ---------- ILBM Writer Support Routines -----------------------------*/
  128.  
  129. /* Note: Just call PutCk to write a BMHD, GRAB, DEST, SPRT, or CAMG
  130.  * chunk. As below. */
  131. #define PutBMHD(context, bmHdr)  \
  132.     PutCk(context, ID_BMHD, sizeof(BitMapHeader), (BYTE *)bmHdr)
  133. #define PutGRAB(context, point2D)  \
  134.     PutCk(context, ID_GRAB, sizeof(Point2D), (BYTE *)point2D)
  135. #define PutDEST(context, destMerge)  \
  136.     PutCk(context, ID_DEST, sizeof(DestMerge), (BYTE *)destMerge)
  137. #define PutSPRT(context, spritePrec)  \
  138.     PutCk(context, ID_SPRT, sizeof(SpritePrecedence), (BYTE *)spritePrec)
  139.  
  140. #ifdef FDwAT
  141.  
  142. /* Initialize a BitMapHeader record for a full-BitMap ILBM picture.
  143.  * This gets w, h, and nPlanes from the BitMap fields BytesPerRow, Rows, and
  144.  * Depth. It assumes you want  w = bitmap->BytesPerRow * 8 .
  145.  * CLIENT_ERROR if bitmap->BytesPerRow isn't even, as required by ILBM format.
  146.  *
  147.  * If (pageWidth, pageHeight) is (320, 200), (320, 400), (640, 200), or
  148.  * (640, 400) this sets (xAspect, yAspect) based on those 4 Amiga display
  149.  * modes. Otherwise, it sets them to (1, 1).
  150.  * 
  151.  * After calling this, store directly into the BitMapHeader if you want to
  152.  * override any settings, e.g. to make nPlanes smaller, to reduce w a little,
  153.  * or to set a position (x, y) other than (0, 0).*/
  154. extern IFFP InitBMHdr(BitMapHeader *, struct BitMap *,
  155.             /*  bmHdr,          bitmap  */
  156.      int,     int,         int,              WORD,      WORD);
  157.  /*  masking, compression, transparentColor, pageWidth, pageHeight */
  158.  /*  Masking, Compression, UWORD -- are the desired types, but get
  159.   *  compiler warnings if use them.                       */
  160.  
  161. /* Output a CMAP chunk to an open FORM ILBM write context. */
  162. extern IFFP PutCMAP(GroupContext *, WORD *,   UBYTE);
  163.           /*  context,        colorMap, depth  */
  164.  
  165. /* This procedure outputs a BitMap as an ILBM's BODY chunk with
  166.  * bitplane and mask data. Compressed if bmHdr->compression == cmpByteRun1.
  167.  * If the "mask" argument isn't NULL, it merges in the mask plane, too.
  168.  * (A fancier routine could write a rectangular portion of an image.)
  169.  * This gets Planes (bitplane ptrs) from "bitmap".
  170.  *
  171.  * CLIENT_ERROR if bitmap->Rows != bmHdr->h, or if
  172.  * bitmap->BytesPerRow != RowBytes(bmHdr->w), or if
  173.  * bitmap->Depth < bmHdr->nPlanes, or if bmHdr->nPlanes > MaxAmDepth, or if
  174.  * bufsize < MaxPackedSize(bitmap->BytesPerRow), or if
  175.  * bmHdr->compression > cmpByteRun1. */
  176. extern IFFP PutBODY(
  177.     GroupContext *, struct BitMap *, BYTE *, BitMapHeader *, BYTE *, LONG);
  178.     /*  context,           bitmap,   mask,   bmHdr,         buffer, bufsize */
  179.  
  180. #else /*not FDwAT*/
  181.  
  182. extern IFFP InitBMHdr();
  183. extern IFFP PutCMAP();
  184. extern IFFP PutBODY();
  185.  
  186. #endif FDwAT
  187.  
  188. /* ---------- ILBM Reader Support Routines -----------------------------*/
  189.  
  190. /* Note: Just call IFFReadBytes to read a BMHD, GRAB, DEST, SPRT, or CAMG
  191.  * chunk. As below. */
  192. #define GetBMHD(context, bmHdr)  \
  193.     IFFReadBytes(context, (BYTE *)bmHdr, sizeof(BitMapHeader))
  194. #define GetGRAB(context, point2D)  \
  195.     IFFReadBytes(context, (BYTE *)point2D, sizeof(Point2D))
  196. #define GetDEST(context, destMerge)  \
  197.     IFFReadBytes(context, (BYTE *)destMerge, sizeof(DestMerge))
  198. #define GetSPRT(context, spritePrec)  \
  199.     IFFReadBytes(context, (BYTE *)spritePrec, sizeof(SpritePrecedence))
  200.  
  201. /* GetBODY can handle a file with up to 16 planes plus a mask.*/
  202. #define MaxSrcPlanes 16+1
  203.  
  204. #ifdef FDwAT
  205.  
  206. /* Input a CMAP chunk from an open FORM ILBM read context.
  207.  * This converts to an Amiga color map: 4 bits each of red, green, blue packed
  208.  * into a 16 bit color register.
  209.  * pNColorRegs is passed in as a pointer to a UBYTE variable that holds
  210.  * the number of ColorRegisters the caller has space to hold. GetCMAP sets
  211.  * that variable to the number of color registers actually read.*/
  212. extern IFFP GetCMAP(GroupContext *, WORD *,   UBYTE *);
  213.           /*  context,        colorMap, pNColorRegs  */
  214.  
  215. /* GetBODY reads an ILBM's BODY into a client's bitmap, de-interleaving and
  216.  * decompressing.
  217.  *
  218.  * Caller should first compare bmHdr dimensions (rowWords, h, nPlanes) with
  219.  * bitmap dimensions, and consider reallocating the bitmap.
  220.  * If file has more bitplanes than bitmap, this reads first few planes (low
  221.  * order ones). If bitmap has more bitplanes, the last few are untouched.
  222.  * This reads the MIN(bmHdr->h, bitmap->Rows) rows, discarding the bottom
  223.  * part of the source or leaving the bottom part of the bitmap untouched.
  224.  *
  225.  * GetBODY returns CLIENT_ERROR if asked to perform a conversion it doesn't
  226.  * handle. It only understands compression algorithms cmpNone and cmpByteRun1.
  227.  * The filed row width (# words) must agree with bitmap->BytesPerRow.
  228.  *
  229.  * Caller should use bmHdr.w; GetBODY only uses it to compute the row width
  230.  * in words. Pixels to the right of bmHdr.w are not defined.
  231.  *
  232.  * [TBD] In the future, GetBODY could clip the stored image horizontally or
  233.  * fill (with transparentColor) untouched parts of the destination bitmap.
  234.  *
  235.  * GetBODY stores the mask plane, if any, in the buffer pointed to by mask.
  236.  * If mask == NULL, GetBODY will skip any mask plane. If
  237.  * (bmHdr.masking != mskHasMask) GetBODY just leaves the caller's mask alone.
  238.  *
  239.  * GetBODY needs a buffer large enough for two compressed rows.
  240.  * It returns CLIENT_ERROR if bufsize < 2 * MaxPackedSize(bmHdr.rowWords * 2).
  241.  *
  242.  * GetBODY can handle a file with up to MaxSrcPlanes planes. It returns
  243.  * CLIENT_ERROR if the file has more. (Could be due to a bum file, though.)
  244.  * If GetBODY fails, itt might've modified the client's bitmap. Sorry.*/
  245. extern IFFP GetBODY(
  246.     GroupContext *, struct BitMap *, BYTE *, BitMapHeader *, BYTE *, LONG);
  247.     /*  context,           bitmap,   mask,   bmHdr,         buffer, bufsize */
  248.  
  249. /* [TBD] Add routine(s) to create masks when reading ILBMs whose
  250.  * masking != mskHasMask. For mskNone, create a rectangular mask. For
  251.  * mskHasTransparentColor, create a mask from transparentColor. For mskLasso,
  252.  * create an "auto mask" by filling transparent color from the edges. */
  253.  
  254. #else /*not FDwAT*/
  255.  
  256. extern IFFP GetCMAP();
  257. extern IFFP GetBODY();
  258.  
  259. #endif FDwAT
  260.  
  261. #endif ILBM_H
  262.  
  263.